home *** CD-ROM | disk | FTP | other *** search
- Path: muss.CIS.McMaster.CA!not-for-mail
- From: u9010255@muss.cis.McMaster.CA (S. Jon)
- Newsgroups: comp.lang.c
- Subject: suspicious pointer conversion warning
- Date: 8 Mar 1996 11:16:29 -0500
- Organization: McMaster University, Hamilton, Ontario, Canada.
- Message-ID: <4hpmgt$4uu@muss.CIS.McMaster.CA>
- NNTP-Posting-Host: muss.cis.mcmaster.ca
-
- to all the pointer gods out there:
-
- i cannot figure out what's wrong with my program. i always get a
- suspicious pointer conversion warning when i try to reallocate more memory.
-
- the purpose of my program is to open a text file and put all the words
- into an array. i initially set my array for 20 words and if there is
- more than 20 words, i increase the array with a growth factor of 10 by
- using realloc.
-
- i would appreciate any input from anyone. pointers and i just don't
- mix! i can't wait 'til i try linked lists!!!! :)
-
- my program is divided into 3 files: wcount.h, wcount.c (main source file),
- wcount2.c (all my functions). compiler is borland c++ v3.1 for dos. here is
- selected parts of my program that i think are relevant:
-
- --------------------------------------------
- // FILENAME: wcount.h
-
- #ifndef WCOUNT_H
- #define WCOUNT_H
-
- typedef struct
- {
- int iFrequency;
- char szWord[1]; // variable length array
- } WordInfo;
-
- // FUNCTION PROTOTYPES
-
- int Parse (FILE *fpFile, WordInfo *apWords[]);
- WordInfo *GetMoreMem (const WordInfo *apWords[], int iWordLimit);
-
- // more function prototypes
-
- #endif
-
- -----------------------------------------
-
- // FILENAME wcount.c
-
- int main ()
- {
- int iWordCount;
- WordInfo *apWords[20]; // array of pointers to structure WordInfo
- FILE *fpFile;
-
- // program opens file
-
- iWordCount = Parse (fpFile, apWords);
-
- // program displays the words
-
- return (0);
- }
-
- ----------------------------------------
-
- // FILENAME wcount2.c
-
- int Parse (FILE *fpFile, WordInfo *apWords[])
- {
- int iWordLimit = 20; // initially limited to 20 words
- int iWordCount; // running count on the number of words in array
-
- // program parses words here and puts word in array with malloc
- // program also compares any new word with words in array to avoid
- // duplication
-
- if (iWordCount == iWordLimit)
- {
- iWordLimit += 10;
-
- // ***** SUSPICIOUS POINTER ERROR REFERS TO LINE BELOW
-
- apWords = GetMoreMem (apWords, iWordLimit);
-
- }
-
- return (iWordCount);
- }
-
- WordInfo *GetMoreMem (const WordInfo *apWords[], int iWordLimit)
- {
- WordInfo *pTemp; // temp pointer to struct WordInfo
-
- pTemp = (WordInfo *)realloc(apWords, iWordLimit*sizeof(WordInfo));
-
- if (pTemp == NULL)
- {
- exit (EXIT_FAILURE);
- }
- return (pTemp);
- }
- --------------------------------------------
-
- thanks for reading it. :)
-
- s
-